﻿<% if (Page_Is_vEdit){ %>

<div>
    <!--就是用来占位的-->
    <div class="contextMenu" id="myMenu_Img">
        <ul>
            <li id="changeImg" style="text-align:center;"><i class="fa fa-edit"></i> 更换图片</li>
        </ul>
    </div>

    <div class="contextMenu" id="myMenu_Img_Cont">
        <ul>
            <li id="changeImg" style="text-align:center;"><i class="fa fa-edit"></i> 更换图片</li>
            <li id="changeTitle" style="text-align:center;"><i class="fa fa-edit"></i> 修改标题</li>
        </ul>
    </div>

    <div class="contextMenu" id="myMenu_Href_Cont">
        <ul>
            <li id="changeHref" style="text-align:center;"><i class="fa fa-edit"></i> 更换链接</li>
        </ul>
    </div>

    <input type="file" style="display:none;" class="file-upload" />
</div>

<link href="/admin/assets/feditor/css/loadfeditor.css" rel="stylesheet" />
<script src="/admin/assets/feditor/js/loadfeditor.js"></script>

<script src="/SysRes/config/global.config.js"></script>
<script src="/SysRes/plugin/layer/layer.js"></script>
<script src="/admin/assets/custom/plugins/jquery.contextmenu.js"></script>
<script src="/admin/assets/custom/js/upload.js"></script>
<script type="text/javascript">
    $(function () {
        var save_box = '<div style="height:78px;"></div><div style="position:fixed;bottom:0;width:100%; text-align:center; z-index:999; padding: 18px 0;background-color: #333;"><input type="button" class="visual-edit-save-btn" value="保存" style="width: 130px;height: 42px;font-size: 16px; background-color: #36c6d3;color:#ffffff;border:none;" /></div>';
        $("body").append(save_box);// 添加保存按钮

        //$("[data-visual]").attr("contenteditable", "true"); //设置可以编辑
        $("[data-visual]").attr("style", "outline: auto;outline-color: #c1c1c1;outline-style: dashed;outline-width: 1px;"); //设置可视化编辑的虚线
        

        //初始化可视化编辑
        var img_obj;
        function Init() {
            InitFeditorInline($("[data-visual]"));
            $("[data-visual]").each(function (i, e) {
                console.log($(e).attr("data-visual-type").toUpperCase());
                switch ($(e).attr("data-visual-type").toUpperCase()) {
                    case "HTML":
                        InitFeditorInline($(e));
                        break;
                    case "TEXT":
                    case "IMAGE":
                    case "BACKGROUNDIMG":
                        $(e).attr("contenteditable", "true");
                        break;
                    default:
                        $(e).attr("contenteditable", "true");
                        break;
                }
            });

        }
        Init();
        //初始化可视化编辑

        //获取某一项的值 , 通常是通过标签的type来获取
        function GetFieldValue(obj) {
            var value = "";
            switch (obj.attr("data-visual-type").toUpperCase()) {
                case "HTML":
                    value = obj.froalaEditor('html.get', true);//obj.html();
                    break;
                case "TEXT":
                    //value = obj.text().replace(/\n/g, "").replace(/\t/g, "").replace(/\r/g, "").replace(/\r\n/g, "").replace(/&nbsp;/g, " ");
                    value = obj.html().replace(/<[^>]+>/g, "").replace(/\n/g, "").replace(/\t/g, "").replace(/\r/g, "").replace(/\r\n/g, "").replace(/&nbsp;/g, " ").trim();
                    break;
                case "IMAGE":
                    value = obj.attr("src");
                    break;
                case "BACKGROUNDIMG":
                    value = obj.css("background-image");
                    value = value.Trim("url").replace("(", "").replace(")", "").trim("\"").trim("'");
                    break;
                default:
                    break;
            }
            return value;
        }

        //获取可视化编辑的基本信息 , JSON格式 , 拿到所有表的所有字段 , 供后面使用
        function GetVisualBaseInfo() {
            var VisualBaseInfoList = new Array();
            var VisualBaseInfo = {};
            $("[data-visual]").each(function (i, e) {
                VisualBaseInfo = {};
                VisualBaseInfo.Table = $(e).attr("data-visual-table");
                VisualBaseInfo.Id = $(e).attr("data-visual-id");
                VisualBaseInfo.ClassId = $(e).attr("data-visual-id");
                VisualBaseInfo.Field = $(e).attr("data-visual-field");
                VisualBaseInfo.Value = GetFieldValue($(e));
                //contenteditable="true"
                //去除可视化编辑生成的编辑标签
                VisualBaseInfo.Value = (VisualBaseInfo.Value).replace(/ contenteditable="true" /ig, " ").replace(/ contenteditable="false" /ig, " ");
                VisualBaseInfoList.push(VisualBaseInfo);
            });
            console.log(VisualBaseInfoList);
            return VisualBaseInfoList;
        }



        //根据某个JSON获取某个字段的所有值 (去除重复项)
        function GetDataList(jsonData, field) {
            var list = new Array();
            $.each(jsonData, function (i, e) {
                var cur = e[field];
                if (list.indexOf(cur) == -1) {
                    list.push(cur);
                }
            });
            return list;
        }

        //nouse
        function GetDataList(jsonData) {
            var list = new Array();
            var list_cont, list_cont_model;

            $.each(jsonData, function (i, e) {
                var cur = e["Table"];
                if (list.indexOf(cur) == -1) {
                    //没有相关的表
                    list.push(cur);
                }
            });
            return list;
        }

        //console.log(GetVisualBaseInfo());

        //遍历DOM生成的json
        //通过table和id 判断是否存在当前的数据

        $("body").delegate(".visual-edit-save-btn", "click", function () {
            _this = $(this);
            var data = GetVisualBaseInfo();
            data = StrEnCode(JSON.stringify(data));
            data = { FieldList: data };
            //console.log(FieldList);
            _this.attr("disabled", "disable");
            layer.load(2, { shade: 0.15 });
            AjaxSubmit(appConfig.adminPath + "ashx/visualedit.ashx", "vEdit", data, function (f, d) {
                //console.log(f, d);
                layer.closeAll();
                if (f) {
                    //请求成功
                    if (parseInt(d.result) > 0) {
                        //alert("操作成功");
                        layer.alert('内容修改成功', { icon: 1,title:appConfig.toastrTitle }, function () {
                            layer.closeAll();
                            setTimeout(function () { location.reload(); }, 500);
                        });
                        
                    }
                    else if (parseInt(d.result) == -99) {
                        //alert("登录超时 , 请重新登录");
                        layer.alert('登录超时 , 请重新登录', { icon: 7, title: appConfig.toastrTitle }, function () {
                            layer.closeAll();
                            setTimeout(function () { window.location.href = appConfig.adminPath + "login.aspx?from=" + encodeURIComponent(window.location.href) }, 500);
                        });
                        //alert("登录超时 , 请重新登录", function () {
                        //    setTimeout(function () {
                        //        JumpLogin();
                        //    }, 500);
                        //});
                    }
                    else if (parseInt(d.result) == -100) {
                        //alert("权限不足 , 无法操作 !");
                        layer.msg("权限不足 , 无法操作", { icon: 4 });
                    }
                    else {
                        //alert("操作失败 , 请稍后重试 !");
                        layer.msg("操作失败 , 请稍后刷新重试 !", { icon: 7 });
                    }
                }
                else {
                    //请求失败
                    //alert("请求出错 , 请稍后刷新重试 !");
                    layer.msg("请求出错 , 请稍后刷新重试 !", { icon: 2 });
                }

                _this.removeAttr("disabled");
            });
            //console.log(GetVisualBaseInfo())
            //alert("保存成功");
        });


    });
</script>
<%} %>